﻿#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(800,600);
    initTable();
   this->setWindowFlags(Qt::FramelessWindowHint);  //设置无边框

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    drawRect(painter);
    // 画小圆
    drawSmallCricle(painter,60);
    //画刻度
    drawScale(painter,height()/2);
    // 写刻度文字
    drawText(painter);
    // 画指针
    drawPointer(painter);
    //画扇形
    drawPie(painter,height()/2+15);
    //画内圈发光效果
    drawInnerShine(painter, 110);
    // 画黑色内圈
    drawBlackCricle(painter,80);
    // 当前速度
    drawCurrendSpeed(painter);
    // 外部发光圈
    drawOutShine(painter,height()/2+20);
    // 画logo
    drawLogo(painter,height()/2);
}

void Widget::initTable()
{
    timer = new QTimer(this);
    Angle = 0;
    mark = 0;
    connect(timer,&QTimer::timeout,[=](){
        if(mark == 0){
            Angle++;
            if(Angle > 60){
                mark = 1;
            }
        }
        if(mark == 1){
            Angle--;
            if(Angle == 0){
                mark = 0;
            }
        }
        update();
    });
    timer->setInterval(50);
    timer->start();
}

void Widget::drawBigCricle(QPainter &painter)
{
    /* QRadialGradient radialGradient(0,0,height()/2);
    radialGradient.setColorAt(0,QColor(255,0,0,50));
    radialGradient.setColorAt(1,QColor(255,0,0,255));
    // 画大圆
    painter.setBrush(radialGradient);
    painter.drawEllipse(QPoint(0,0),height()/2,height()/2);*/
}

void Widget::drawSmallCricle(QPainter &painter, int radius)
{
    //坐标系 平移到中心
    QPoint cent(rect().width()/2,rect().height()*0.6);
    painter.translate(cent);
    // 画小圆
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(Qt::white,3));
    painter.drawEllipse(QPoint(0,0),radius,radius);



}

void Widget::drawScale(QPainter &painter, int Radius)
{

    //设置第一个刻度的位置
    painter.save();//保存原点，X在3点钟方向
    painter.rotate(150);
//    painter.setFont(QFont("华文宋体",6));
    painter.setPen(QPen(Qt::white,5));
    for(int i=0;i<=60;i++){
        if(i>=40){
            painter.setPen(QPen(Qt::red,5));
        }
        if(i%5 == 0){
            //画刻度线
            painter.drawLine(Radius-3,0,Radius-20,0);
        }else {
            painter.drawLine(Radius-3,0,Radius-10,0);
        }
        painter.rotate(angle);
    }
    painter.restore();
}

void Widget::drawText(QPainter &painter)
{
    int r = height()/2-45;
painter.setFont(QFont("Arial",16));
    for(int i=0;i<=60;i++){
        if(i%5 == 0){
            // 保存坐标系
            painter.save();
            // 算出平移点
            int delx = qCos(qDegreesToRadians(210-angle*i))*r;
            int dely = qSin(qDegreesToRadians(210-angle*i))*r   ;
            // 平移坐标系
            painter.translate(QPoint(delx,-dely));
            // 旋转坐标系
            painter.rotate(angle*i-120); //i=30时，angle*i=0,
            // 写刻度
            painter.drawText(QRect(-25,-25,50,30),Qt::AlignCenter,QString::number(i*4));
            // 恢复坐标系
            painter.restore();
        }
    }
}

void Widget::drawPointer(QPainter &painter)
{
    painter.save();
    painter.rotate(150+Angle*angle);
    static const QPointF points[3] = {
        QPointF(0, 8.0),
        QPointF(300.0, 0),
        QPointF(0, -8.0),
    };

    painter.drawPolygon(points, 3);

    painter.restore();//恢复原点
}

void Widget::drawPie(QPainter &painter,int raidus)
{

    QRect rentangle(-raidus,-raidus,raidus*2,raidus*2);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255,0,0,70));
    painter.drawPie(rentangle,(360-150)*16,-Angle*angle*16);
}

void Widget::drawRect(QPainter &painter)
{
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setBrush(Qt::black);
    painter.drawRect(rect());
}

void Widget::drawBlackCricle(QPainter &painter, int radius)
{
    painter.setBrush(Qt::black);
    painter.setPen(QPen(Qt::black,3));
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

void Widget::drawCurrendSpeed(QPainter &painter)
{
    painter.setPen(Qt::white);
    QFont font("Arial",25);
    font.setBold(true);
    painter.setFont(font);
    painter.drawText(QRect(-60,-60,120,70),Qt::AlignCenter,QString::number(Angle*4));

    QFont font2("Arial",20);
    font2.setBold(false);
    painter.setFont(font2);
    painter.drawText(QRect(-60,-60,120,140),Qt::AlignCenter,"Km/h");
}

void Widget::drawInnerShine(QPainter &painter, int radius)
{
    QRadialGradient radialGradient(0,0,radius);
    radialGradient.setColorAt(0.1,QColor(255,0,0,250));
    radialGradient.setColorAt(1,QColor(255,0,0,30));
    painter.setBrush(radialGradient);
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

void Widget::drawOutShine(QPainter &painter, int radius)
{

    QRect rentangle(-radius,-radius,radius*2,radius*2);
    painter.setPen(Qt::NoPen);

    QRadialGradient radialGradient(0,0,radius);
    radialGradient.setColorAt(1,QColor(255,0,0,255));
    radialGradient.setColorAt(0.9,QColor(255,0,0,0));
    radialGradient.setColorAt(0.1,QColor(255,0,0,0));
    painter.setBrush(radialGradient);

    painter.drawPie(rentangle,(360-150)*16,-angle*60*16);
}

void Widget::drawLogo(QPainter &painter, int radius)
{
    QRect rect(-40,0.5*radius,100,80);
    painter.drawPixmap(rect,QPixmap(":/logo/car.png"));
}

